﻿using CommonDevelop.Operator;
using Microsoft.AspNetCore.Http;
using System;

namespace WebWap
{
    public class OperatorScene : IOperator
    {
        public void DeleteCookies(string key)
        {
            var HC = JwtHelper.GetHttpContext();
            if (HC != null)
                HC.Response.Cookies.Delete(key);
        }

        public string GetCookies(string key)
        {
            var HC = JwtHelper.GetHttpContext();
            if (HC == null)
                return string.Empty;

            HC.Request.Cookies.TryGetValue(key, out string value);
            if (string.IsNullOrEmpty(value))
                value = string.Empty;

            return value;
        }

        public string GetSession(string key)
        {
            var HC = JwtHelper.GetHttpContext();
            if (HC == null)
                return string.Empty;

            var value = HC.Session.GetString(key);
            if (string.IsNullOrEmpty(value))
                value = string.Empty;

            return value;
        }

        public void SessionClear()
        {
            var HC = JwtHelper.GetHttpContext();
            if (HC != null)
                HC.Session.Clear();
        }

        public void SetCookies(string key, string value, int minutes = 480)
        {
            var HC = JwtHelper.GetHttpContext();
            if (HC == null)
                return;

            HC.Response.Cookies.Append(key, value, new CookieOptions
            {
                HttpOnly = false,
                Secure = false,
                IsEssential = true,
                Expires = DateTime.Now.AddMinutes(minutes)
            });
        }

        public void SetSession(string key, string value)
        {
            var HC = JwtHelper.GetHttpContext();
            if (HC != null)
                HC.Session.SetString(key, value);
        }
    }
}
